#include "application.h"
#include "semphr.h"
#include "dispDirver.h"
#include "bsp_usart.h"
extern int control,sendFlag,controlFlag;
extern TaskHandle_t task_handle4;
//extern SemaphoreHandle_t ControlMu_Handle;
void White_Dark_Day(xpItem item)
{
    Set_BgColor(item->switchState);
}

//const unsigned char symbol[][16] = {
//	0x00,0x1C,0xE0,0x18,0x04,0x08,0x30,0x00,
//	0x00,0x00,0x07,0x18,0x20,0x20,0x10,0x00,/*"℃",0*/
	
//	0xF0,0x08,0xF0,0x80,0x60,0x18,0x00,0x00,
//	0x00,0x31,0x0C,0x03,0x1E,0x21,0x1E,0x00/*"%",1*/
//};

void Show_Data(xpItem item)
{
	extern double data[4];
    char A[20], B[20], C[20],D[20];

    if (DialogScale_Show(1, 2, 100, 62))
    {
        sprintf(A, "Temp = %.2f", data[0]);
        sprintf(B, "Hum= %.2f", data[1]);
        sprintf(C, "Ohum= %.2f", data[2]);
		sprintf(D, "Inten = %.2f", data[3]);

        OLED_DrawStr(6, 14, A);
        OLED_DrawStr(6, 29, B);
        OLED_DrawStr(6, 44, C);
		OLED_DrawStr(6, 59, D);

		
        OLED_SendBuffer();
    }

}

void Show_Text(xpItem item)
{
    const char* test = "xxxxxx";

    if(DialogScale_Show(22, 24, 82, 20))
    {
        OLED_DrawStr(28, 37, test);
        OLED_SendBuffer();
    }
}

void Setting1(xpItem item)//-20-50 2
{
    char value[20] = {0};
    
    if (DialogScale_Show(4, 12, 120, 32))
    {

        switch (item->state)
        {
        case MENU_UP:
            (*item->data.ptr) = Draw_Scrollbar(8, 30, 90, 6, 3, -20, 50, 2, (*item->data.ptr));
            break;
        case MENU_DOWN:
            (*item->data.ptr) = Draw_Scrollbar(8, 30, 90, 6, 3, -20, 50, -2, (*item->data.ptr));
        default:
            Draw_Scrollbar(8, 30, 90, 6, 3, -20, 100, 0, (*item->data.ptr));
            break;
        }
		
        sprintf(value, "Value: %d ", (*item->data.ptr));
        OLED_DrawStr(8, 25, value);
        OLED_SendBuffer();
        sendFlag = 2;
    }

}
void Setting2(xpItem item)//0-100 5
{
    char value[20] = {0};
    
    if (DialogScale_Show(4, 12, 120, 32))
    {

        switch (item->state)
        {
        case MENU_UP:
            (*item->data.ptr) = Draw_Scrollbar(8, 30, 90, 6, 3, 0, 100, 5, (*item->data.ptr));
            break;
        case MENU_DOWN:
            (*item->data.ptr) = Draw_Scrollbar(8, 30, 90, 6, 3, 0, 100, -5, (*item->data.ptr));
        default:
            Draw_Scrollbar(8, 30, 90, 6, 3, 0, 100, 0, (*item->data.ptr));
            break;
        }

        sprintf(value, "Value: %d ", (*item->data.ptr));
        OLED_DrawStr(8, 25, value);
        OLED_SendBuffer();
        sendFlag = 2;
    }
}

void Setting3(xpItem item)//0-1000 10
{
    char value[20] = {0};
    
    if (DialogScale_Show(4, 12, 120, 32))
    {

        switch (item->state)
        {
        case MENU_UP:
            (*item->data.ptr) = Draw_Scrollbar(8, 30, 90, 6, 3, 0, 1000, 10, (*item->data.ptr));
            break;
        case MENU_DOWN:
            (*item->data.ptr) = Draw_Scrollbar(8, 30, 90, 6, 3, 0, 1000, -10, (*item->data.ptr));
        default:
            Draw_Scrollbar(8, 30, 90, 6, 3, 0, 1000, 0, (*item->data.ptr));
            break;
        }

        sprintf(value, "Value: %d ", (*item->data.ptr));
        OLED_DrawStr(8, 25, value);
        OLED_SendBuffer();
        sendFlag = 2;
    }
}
void Setting4(xpItem item)//0-100 10
{
    char value[20] = {0};
    
    if (DialogScale_Show(4, 12, 120, 32))
    {

        switch (item->state)
        {
        case MENU_UP:
            (*item->data.ptr) = Draw_Scrollbar(8, 30, 90, 6, 3, 0, 100, 10, (*item->data.ptr));
            break;
        case MENU_DOWN:
            (*item->data.ptr) = Draw_Scrollbar(8, 30, 90, 6, 3, 0, 100, -10, (*item->data.ptr));
        default:
            Draw_Scrollbar(8, 30, 90, 6, 3, 0, 100, 0, (*item->data.ptr));
            break;
        }
        sprintf(value, "Value: %d ", (*item->data.ptr));
        OLED_DrawStr(8, 25, value);
        OLED_SendBuffer();
        sendFlag = 3;
    }
}
const unsigned char logo[] = { 
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x7F,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0xFF,0x00,0x00,0x00,0x00,0x00,
0x00,0xF0,0xFF,0x01,0x00,0x00,0x00,0x00,0x00,0xFE,0xFF,0x07,0x00,0x00,0x00,0x00,
0x80,0x3F,0xFF,0x1F,0x00,0x00,0x00,0x00,0xC0,0x03,0xFE,0x3F,0x00,0x00,0x00,0x00,
0x00,0x00,0xFE,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0xFF,0x01,0x00,0x00,0x00,
0x00,0xF0,0xFF,0xFF,0x07,0x00,0x00,0x00,0x80,0xFF,0xFF,0xFF,0x0F,0x00,0x00,0x00,
0x00,0xFF,0xFF,0xFD,0x3F,0x00,0x00,0x00,0x00,0x00,0xFC,0xF8,0xFF,0x00,0x00,0x00,
0x00,0x00,0x7C,0xE0,0xFF,0x01,0x00,0x00,0x00,0x00,0x3C,0xC0,0xFF,0x07,0x00,0x00,
0x00,0x00,0x08,0x00,0xFF,0xEF,0x1C,0x00,0x00,0x00,0x00,0x00,0xFC,0x3F,0x60,0x00,
0x00,0x00,0x00,0x00,0xF8,0xFF,0xC3,0x00,0x00,0x00,0x00,0x00,0xE0,0xFF,0x9F,0x00,
0x00,0x00,0x00,0x00,0xC0,0xFF,0x3F,0x01,0x00,0x00,0x00,0x00,0x00,0xFF,0x3F,0x01,
0x00,0x00,0x00,0x00,0x00,0xFE,0x7F,0x02,0x00,0x00,0x00,0x00,0x00,0xFE,0x7F,0x02,
0x00,0x00,0x00,0x00,0x80,0xFF,0x7F,0x02,0x00,0x00,0x00,0x00,0xE0,0xFF,0x7F,0x02,
0x00,0x00,0x00,0x00,0xF8,0xFF,0x7F,0x02,0x00,0x00,0x00,0x00,0xFE,0xFF,0x3F,0x01,
0x00,0x00,0x00,0x80,0xFF,0xFF,0x1F,0x01,0x00,0x00,0x00,0xE0,0xFF,0xFF,0x8F,0x00,
0x00,0x00,0x00,0xF0,0xFF,0xFF,0xC0,0x00,0x00,0x00,0x00,0xFC,0xFF,0x3F,0x20,0x00,
0x00,0x80,0x3F,0xFF,0xFF,0xEF,0x1C,0x00,0x00,0xE0,0xFF,0xFF,0xFF,0x03,0x00,0x00,
0x00,0xF0,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0xF0,0xFF,0xFF,0x3F,0x00,0x00,0x00,
0x00,0xF8,0xFF,0xFF,0x0F,0x00,0x00,0x00,0x00,0xF8,0xFF,0xFF,0x03,0x00,0x00,0x00,
0x00,0xF8,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0xF8,0xFF,0x7F,0x00,0x00,0x00,0x00,
0x00,0xF8,0xFF,0x7F,0x00,0x00,0x00,0x00,0x00,0xF8,0xFF,0x7F,0x00,0x00,0x00,0x00,
0x00,0xF0,0xFF,0x7F,0x00,0x00,0x00,0x00,0x00,0xF0,0xFF,0x7F,0x00,0x00,0x00,0x00,
0x00,0xE0,0xFF,0x7F,0x00,0x00,0x00,0x00,0x00,0x80,0xFF,0x7F,0x00,0x00,0x00,0x00,
0x00,0x80,0xFF,0x7F,0x00,0x00,0x00,0x00,0x00,0x80,0xFF,0x7F,0x00,0x00,0x00,0x00,
0x00,0x80,0xFF,0x7F,0x00,0x00,0x00,0x00,0x00,0x80,0xFF,0x7F,0x00,0x00,0x00,0x00,
0x00,0xF8,0xFF,0xFF,0x03,0x00,0x00,0x00,0x00,0xF8,0xFF,0xFF,0x03,0x00,0x00,0x00,
0x00,0xF8,0xFF,0xFF,0x03,0x00,0x00,0x00,0x00,0xF8,0xFF,0xFF,0x03,0x00,0x00,0x00,
0x00,0xE0,0xFF,0x7F,0x00,0x00,0x00,0x00,0x00,0xE0,0xFF,0x7F,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
};

void Show_Log(xpItem item)
{
    OLED_ClearBuffer();
    OLED_DrawXBMP(32, 0, 64, 64, logo);
    OLED_SendBuffer();
}

void Setting_Contrast(xpItem item)
{
    char contrast[20];
    if(DialogScale_Show(4, 12, 120, 32))
    {

        switch (item->state)
        {
        case MENU_UP:
            (*item->data.ptr) = Draw_Scrollbar(8, 30, 90, 6, 3, 0, 255, 5, (*item->data.ptr));
            break;
        case MENU_DOWN:
            (*item->data.ptr) = Draw_Scrollbar(8, 30, 90, 6, 3, 0, 255, -5, (*item->data.ptr));
            break;
        default:
            Draw_Scrollbar(8, 30, 90, 6, 3, 0, 255, 0, (*item->data.ptr));
            break;
        }

        sprintf(contrast, "Contrast = %d", (*item->data.ptr));
        OLED_DrawStr(8, 25, contrast);

        OLED_SetContrast((*item->data.ptr));
        OLED_SendBuffer();
    }
}

void PowerSave(xpItem item)
{
    OLED_SetPowerSave(item->switchState);
}
void Led(xpItem item)
{
	int x = control&0x0001;
    if(x== 0x0000){
		//TIM_SetCompare1(TIM1,*item->data.ptr);
		control |=0x0001;
        xTaskNotifyGive(task_handle4);
        //xSemaphoreGive(ControlMu_Handle);
    }else if(x == 0x0001){
        //TIM_SetCompare1(TIM1,0);
		control &= 0xFFF0;
        xTaskNotifyGive(task_handle4);
        //xSemaphoreGive(ControlMu_Handle);
    }
}
void Fan(xpItem item)
{
	int x = control&0x0010;
    if(x == 0x0000){
		//TIM_SetCompare4(TIM1,*item->data.ptr);
		control |=0x0010;
        xTaskNotifyGive(task_handle4);
    }else if(x == 0x0010){
		control &= 0xFF0F;
        xTaskNotifyGive(task_handle4);
    }
}
void Pump(xpItem item)
{
	int x = control&0x0100;
	if(x == 0x0000){
		//TIM_SetCompare1(TIM1,*item->data.ptr);
		control |=0x0100;
        controlFlag = 2;
        xTaskNotifyGive(task_handle4);
	}else if(x== 0x0100){
		//TIM_SetCompare1(TIM1,0);
		control &= 0xF0FF;
        xTaskNotifyGive(task_handle4);
	}
}
void Con(xpItem item){
    if(controlFlag == 1) controlFlag = 2;
    else controlFlag = 1;
}

